//
//  fenLeiDetailShaiXuanView.m
//  Experss
//
//  Created by Chen on 2017/8/3.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "fenLeiDetailShaiXuanView.h"

#import "FenleiShaixuanModel.h"
#import "FenleiShaixuanSmallModel.h"

#import "fenLeiDetailShaiXuanCollectionTableViewCell.h"

#import "fenLeiDetialPriceTableViewCell.h"

@interface fenLeiDetailShaiXuanView ()<UITableViewDelegate,UITableViewDataSource>

/**  */
@property (nonatomic , strong) UIView *slidView;

/**  */
@property (nonatomic , strong) UITableView *tableView;
//存储筛选结果
@property (nonatomic , strong) NSMutableArray *shaiXuanBrandArr;
@property (nonatomic, strong) NSMutableArray *shaiXuanPriceArr;

@end

@implementation fenLeiDetailShaiXuanView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        _shaiXuanBrandArr = [NSMutableArray array];
        _shaiXuanPriceArr = [NSMutableArray array];
        [self createSubviews];
    }
    
    return self;
    
}

- (void)createSubviews{
    
    CGFloat ww = 300;
    
    self.slidView = [[UIView alloc] initWithFrame:CGRectMake(kScreen_Width, 0, ww, kScreen_Height)];
    
    [self addSubview:self.slidView];
    
    // 创建头部
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ww, 35)];
    headerView.backgroundColor = kRGB(241, 245, 250);
    [self.slidView addSubview:headerView];
    
    UILabel *headerlabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 35)];
    headerlabel.text = @"筛选";
    headerlabel.textAlignment = NSTextAlignmentLeft;
    headerlabel.textColor = [UIColor grayColor];
    headerlabel.font = [UIFont systemFontOfSize:16];
    [headerView addSubview:headerlabel];
    
    // 创建tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headerView.frame), ww, kScreenHeight-35-50) style:(UITableViewStyleGrouped)];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.bounces = YES;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [tableView registerNib:[UINib nibWithNibName:@"fenLeiDetialPriceTableViewCell" bundle:nil] forCellReuseIdentifier:@"pcell"];
    
    [tableView registerClass:[fenLeiDetailShaiXuanCollectionTableViewCell class] forCellReuseIdentifier:@"ccell"];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.slidView addSubview:tableView];
    self.tableView = tableView;
    
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.frame = CGRectMake(0, kScreen_Height-50, 150, 50);
//    btn.backgroundColor = RGBAColor(250, 155, 85, 1);
    [btn setBackgroundImage:[UIImage imageNamed:@"chengkuai"] forState:UIControlStateNormal];
    [btn setTitle:@"重置" forState:(UIControlStateNormal)];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(chongzhi) forControlEvents:(UIControlEventTouchUpInside)];
    [self.slidView  addSubview:btn];
    
    
    UIButton *btn2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn2.frame = CGRectMake(150, kScreen_Height-50, 150, 50);
//    btn2.backgroundColor = RGBAColor(80, 165, 150, 1);
    [btn2 setTitle:@"完成" forState:(UIControlStateNormal)];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"bg1"] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn2 setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [btn2 addTarget:self action:@selector(queding) forControlEvents:(UIControlEventTouchUpInside)];
    [self.slidView  addSubview:btn2];
    
    [UIView animateWithDuration:0.7 animations:^{
       
        self.slidView.frame = CGRectMake(kScreen_Width-300, 0, 300, kScreen_Height);
        
    }];
    
}

- (void)chongzhi{
    
    for (FenleiShaixuanModel *model in self.dataArray) {
        NSArray *BrandArr = model.brand;
        for (FenleiShaixuanSmallModel *model2 in BrandArr) {
            model2.isSelect = NO;
        }
    }
    [self.tableView reloadData];
    
    
    
    //取消筛选
    [_shaiXuanBrandArr removeAllObjects];
    [_shaiXuanPriceArr removeAllObjects];
}

- (void)queding{
    
    // 获取品牌
    FenleiShaixuanModel *model1 = self.dataArray[0];
    NSArray *BrandArr = model1.brand;
    for (FenleiShaixuanSmallModel *model2 in BrandArr) {
        
        if (model2.isSelect) {
            [_shaiXuanBrandArr addObject:model2.Id];
        }
    }
    // 获取价格
    FenleiShaixuanModel *modle2 = self.dataArray[1];
    NSLog(@"最高价 : %@",modle2.maxprice);
    NSLog(@"最低价 : %@",modle2.minprice);
    if (modle2.maxprice.integerValue < modle2.minprice.integerValue) {
        [MBProgressHUD showText:@"最到价不能低于最低价"];
        return;
    }

    if (modle2.maxprice.length && modle2.minprice.length) {
        [_shaiXuanPriceArr addObject:modle2.maxprice];
        [_shaiXuanPriceArr addObject:modle2.minprice];

    }
    
    if ([self.delegate respondsToSelector:@selector(fenLeiDetailShaiXuanBackBrandArr:PriceArr:)]) {
        [self.delegate fenLeiDetailShaiXuanBackBrandArr:_shaiXuanBrandArr PriceArr:_shaiXuanPriceArr];
    }
    [self removeFromSuperview];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    FenleiShaixuanModel *model = self.dataArray[section];
    if (model.isopen == YES) {
        return 1;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FenleiShaixuanModel *model = self.dataArray[indexPath.section];
    
    return [self heightWithModel:model];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FenleiShaixuanModel *model = self.dataArray[indexPath.section];
    
    if ([model.type isEqualToString:@"0"]) {
        fenLeiDetailShaiXuanCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ccell" forIndexPath:indexPath];
        cell.dataArray = model.brand;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    } else{
        fenLeiDetialPriceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pcell" forIndexPath:indexPath];
        
        cell.model = model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }

}


// 根据 model 计算 cell高度
- (CGFloat)heightWithModel:(FenleiShaixuanModel *)model{
    // 判断类型
    // 比如  0   是 collectionView
    //      1   是  价格
    if ([model.type isEqualToString:@"0"]) {
        //计算高度
        NSInteger i = model.brand.count/3;
        
        if (model.brand.count % 3 != 0) {
            i++;
        }

        return 50*i + 10*(i+1);
        
    } else {
        return 50;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    FenleiShaixuanModel *model = self.dataArray[section];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
    view.backgroundColor = [UIColor whiteColor];
    view.tag = 3000+section;
    [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeader:)]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 15)];
    label.text = model.name;
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(270, 15, 15, 10)];
//    imageView.image = [UIImage imageNamed:@"shaixuan_shangjiantou"];//shaixuan_xiajiantou
//    imageView.backgroundColor = [UIColor cyanColor];
    [view addSubview:imageView];
    
    
    if (model.isopen == YES) {
        imageView.image = [UIImage imageNamed:@"shaixuan_shangjiantou"];
    } else{
        imageView.image = [UIImage imageNamed:@"shaixuan_xiajiantou"];
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 20;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 19, 300, 1)];
    lineView.backgroundColor = BaseColor;
    
    [view addSubview:lineView];
    
    return view;
}


- (void)tapHeader:(UITapGestureRecognizer *)tap{
    NSInteger index = tap.view.tag - 3000;
    
    FenleiShaixuanModel *model = self.dataArray[index];
    
    model.isopen = !model.isopen;
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:(UITableViewRowAnimationFade)];
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    
    
    
    [self.tableView reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
